﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using BusinessLogicLibrary;
using entities;

namespace ServiceLibrary
{
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class ExternalService : IExternalService
    {
        private readonly BookingModule _booking = new BookingModule();
        private readonly CustomerModule _customer = new CustomerModule();
        private readonly PromotionModule _promotion = new PromotionModule();
        private readonly SearchModule _search = new SearchModule();
        private readonly RoomModule _room = new RoomModule();

        #region IExternalService Members

        public List<RoomType> RoomTypeGets()
        {
            try
            {
                return _room.GetRoomTypes();
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        public List<CheckIn> SelectCheckingHistory(int cusID)
        {
            return _booking.SelectCheckingHistory(cusID);
        }

        public List<vTotalCheck> SelectTotalCheck(DateTime date, int check)
        {
            return _booking.SelectTotalCheck(date, check);
        }

        public List<Promotion> GetListPromotion()
        {
            return _promotion.GetListPromotion();
        }

        public List<PromotionDetail> GetPromotionDetails(int proID)
        {
            return _promotion.GetPromotionDetailsbyProID(proID);
        }

        public List<vSubPromotion> PromotionsGet(bool available)
        {
            try
            {
                return _promotion.GetPromotions(available);
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        public List<vSubPromotionDetail> PromotionDetailsGetByPromotionId(int promotionId)
        {
            try
            {
                return _promotion.GetPromotionDetailsByPromotionId(promotionId);
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        #endregion

        #region "CUSTOMER Management"

        public bool CustomerEmailCheck(string email)
        {
            try
            {
                return _customer.CheckCustomerEmail(email);
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        public Customer CustomerGetByAccount(string customerAccount)
        {
            try
            {
                return _customer.GetCustomerByAccount(customerAccount);
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "adminRoler")]
        public bool CustomerAdd(string pIdentityNumber, string pCustomerName, string pCustomerPhone,
                                string pCustomerEmail, bool pCustomerSex, out string account)
        {
            try
            {
                return _customer.CreateCustomer(pIdentityNumber, pCustomerName, pCustomerPhone, pCustomerEmail,
                                                pCustomerSex, out account);
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        public bool CustomerModify(int pId, string pIdentityNumber, string pCustomerName, string pCustomerPhone,
                                   string pCustomerEmail, bool pCustomerSex)
        {
            try
            {
                return _customer.ModifyCustomer(pId, pIdentityNumber, pCustomerName, pCustomerPhone, pCustomerEmail,
                                                pCustomerSex);
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        #endregion

        #region "Search Room"

        public List<vRoomInfo> RoomsSearch(DateTime inDate, DateTime outDate, int roomTypeId, int minPrice, int maxPrice,
                                           bool view, bool bath, bool balcony)
        {
            try
            {
                return _search.SearchRooms(inDate, outDate, roomTypeId, minPrice, maxPrice, view, bath, balcony);
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        #endregion

        #region "BOOKING"

        public bool BookingAdd(int customerId, List<int> roomId, DateTime checkInDate, DateTime checkOutDate,
                               string employeeAccount, string employeePassword)
        {
            try
            {
                return _booking.CreateBooking(customerId, roomId, checkInDate, checkOutDate, employeeAccount,
                                              employeePassword);
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        #endregion
    }

    [ServiceContract]
    public interface IExternalService
    {
        [OperationContract]
        List<RoomType> RoomTypeGets();
        [OperationContract]
        List<CheckIn> SelectCheckingHistory(int cusID);
        [OperationContract]
        List<vTotalCheck> SelectTotalCheck(DateTime date, int check);


        #region "CUSTOMER Management"

        [OperationContract]
        bool CustomerAdd(string pIdentityNumber, string pCustomerName, string pCustomerPhone, string pCustomerEmail,
                         bool pCustomerSex, out string account);

        [OperationContract]
        bool CustomerModify(int pId, string pIdentityNumber, string pCustomerName, string pCustomerPhone,
                            string pCustomerEmail, bool pCustomerSex);

        [OperationContract]
        bool CustomerEmailCheck(string email);

        [OperationContract]
        Customer CustomerGetByAccount(string customerAccount);

        #endregion

        #region "Search Room"

        [OperationContract]
        List<vRoomInfo> RoomsSearch(DateTime inDate, DateTime outDate, int roomTypeId, int minPrice, int maxPrice,
                                    bool view, bool bath, bool balcony);

        #endregion

        #region "BOOKING"

        [OperationContract]
        bool BookingAdd(int customerId, List<int> roomId, DateTime checkInDate, DateTime checkOutDate,
                        string employeeAccount, string employeePassword);

        #endregion

        #region "Promotion"

        [OperationContract]
        List<Promotion> GetListPromotion();

        [OperationContract]
        List<PromotionDetail> GetPromotionDetails(int proID);

        [OperationContract]
        List<vSubPromotion> PromotionsGet(bool available);

        [OperationContract]
        List<vSubPromotionDetail> PromotionDetailsGetByPromotionId(int promotionId);
        #endregion
    }
}